//
// Copyright 2001-2004 Stottler Henke Associates, Inc.
// All Rights Reserved.
////////////////////////////////////////////////////////////////////////////////

package com.stottlerhenke.simbionic.engine.core;

import java.io.Serializable;

import com.stottlerhenke.simbionic.api.*;
import com.stottlerhenke.simbionic.common.parser.EParamDir;
import com.stottlerhenke.simbionic.engine.SB_SingletonBook;
import com.stottlerhenke.simbionic.engine.file.SB_StreamReader;
import com.stottlerhenke.simbionic.engine.parser.*;
import com.stottlerhenke.simbionic.common.*;

/**
 * An instance of this class describes a single parameter to a behavior,
 * action, or predicate.
 */
public class SB_Parameter implements Serializable  
{
  protected String _name;
  protected EParamDir _dir;
  protected SB_VarType _type;

  // to avoid extra comparisons
  protected boolean _bIn;
  protected boolean _bOut;

  public SB_Parameter() {
  }

  public SB_Parameter( String name, SB_VarType type, EParamDir dir )
  {
    _name = name;
    _type = type;
    _dir = dir;

    _bIn = (_dir == EParamDir.kIn) || (_dir == EParamDir.kInOut);
    _bOut = (_dir == EParamDir.kOut) || (_dir == EParamDir.kInOut);
  }

  public static String DirToString(EParamDir dir){
    if(dir == EParamDir.kIn)
      return "in";
    else if(dir == EParamDir.kOut)
      return "out";
    else if(dir == EParamDir.kInOut)
      return "in/out";
    else
      return "none";
  }
  /**
   * @return the name for this parameter
   */
  public String GetName() { return _name; }

  /**
   * @return the direction for this parameter
   */
  public EParamDir GetDirection() { return _dir; }

  /**
   * @return true if this is an "in" or "in/out" param, otherwise false
   */
  public boolean IsIn(){ return _bIn; }

  /**
   * @return true if this is an "out" or "in/out" param, otherwise false
   */
  public boolean IsOut() { return _bOut; }

  /**
   * @return the type of this parameter
   */
  SB_VarType GetType() { return _type; }

	/**
	 * Loads a single parameter from a stream.
	 * @param reader the stream reader to load from
	 */
  public void load(SB_StreamReader reader,SB_SingletonBook book) throws SB_FileException
	{
  	_name = reader.readPString();

		// read param type
		int type = reader.readInt();
		_type = SB_VarType.getTypeFromInt(type);

		if ((_type.getState() < 0) || 
		    (_type.getState() >= SB_VarType.kLAST_VARTYPE.getState() &&
		     _type.getState() < SB_VarType.kCLASS_START.getState()   ))
		  throw new SB_FileException("Illegal variable type in file");

		// read parameter direction
		int dir = reader.readInt();
		_dir = EParamDir.getTypeFromInt(dir);

		// to save a little time during execution
		_bIn = (_dir == EParamDir.kIn) || (_dir == EParamDir.kInOut);
		_bOut = (_dir == EParamDir.kOut) || (_dir == EParamDir.kInOut);
  }

  public String toString(){
    return new String("Param " + _name + " [" + SB_Variable.TypeToString(_type) +
                "] (" + SB_Parameter.DirToString(_dir) + ")");

  }
}